package com.rocksea.app.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.rocksea.app.R;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.fragment.InspeFormItemFragment;
import com.rocksea.app.fragment.PilNoListFragment;
import com.rocksea.app.fragment.SInspecFromFragment;
import com.rocksea.app.fragment.SPilNoListFragment;
import com.rocksea.app.utils.UiUtils;

public class InpseFromActivity extends AppCompatActivity {

    private Bundle mBundle;
    private int type;
    public static final String KEY = "From_Type";
    public static final int QInspection_From = 1;
    public static final int SInspection_From = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inpse_from_v1);
        mBundle = getIntent().getBundleExtra(UiUtils.BUNDLE_KEY);
        String pileNum = mBundle.getString("PileNum");
        InspectionForm.InspectionFormListBean inspectionForm = mBundle.getParcelable(UiUtils.BUNDLE_KEY);
        type = mBundle.getInt(KEY);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle(pileNum);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.gark_40), ContextCompat.getColor(this,android.R.color.white));
        tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.red_50));
        ViewCompat.setElevation(tabLayout, 10);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            switch (position)
            {
                case 0:
                    switch (type)
                    {
                        case QInspection_From:
                            return InspeFormItemFragment.newInstance(mBundle);
                        case SInspection_From:
                            return SInspecFromFragment.newInstance(mBundle);
                    }
                case 1:
                    switch (type)
                    {
                        case QInspection_From:
                            return PilNoListFragment.newInstance(mBundle);
                        case SInspection_From:
                            return SPilNoListFragment.newInstance(mBundle);
                    }
                default:
                    return InspeFormItemFragment.newInstance(mBundle);
            }
        }
        @Override
        public int getCount() {
            return 2;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "详情";
                case 1:
                    return "测桩";
            }
            return null;
        }
    }
}
